const path_ = eval('require')('path');
const fs = eval('require')('fs');

const mkdirs = {
    // 异步创建，无法保证先后执行回调的顺序
    async(dirname, cb) {
        fs.exists(dirname, (exists) => {
            if (exists) {
                cb();
            } else {
                mkdirs.async(path_.dirname(dirname), () => {
                    fs.mkdir(dirname, cb);
                });
            }
        });
    },
    // 同步创建，可保证顺序
    sync(dirname) {
        if (fs.existsSync(dirname)) {
            return true;
        }
        if (mkdirs.sync(path_.dirname(dirname))) {
            fs.mkdirSync(dirname);
            return true;
        }
    },
};

module.exports = mkdirs;
